package com.bawei.week.park

import androidx.paging.PagingData
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bawei.lib_base.entity.ParkBean
import com.bawei.lib_base.entity.ParkBeanItem
import com.bawei.lib_base.res.Res
import com.bawei.lib_base.utils.ApiService

class ParkDataSource(val apiService: ApiService,val pare : String,val type : String) : PagingSource<Int,ParkBeanItem>(){
    override fun getRefreshKey(state: PagingState<Int,ParkBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ParkBeanItem> {
        try {
            val page = params.key?:1
            var bean : Res<ParkBean>?=null
            if (type == "类型"){
                bean = apiService.getParkType(pare)
            }else if (type == "状态"){
                bean = apiService.getParkStatus(pare)
            }else if (type == "区域"){
                bean = apiService.getParkArea(pare.toInt())
            }else if (type == "车牌号"){
                bean = apiService.getParkLicense(pare)
            }
            val nextPage = if (bean != null) page + 1 else null
            return LoadResult.Page(bean!!.data,null,nextPage)
        }catch (e : Exception){
            return LoadResult.Error(e)
        }
    }
}